package com.tedu.openmedical.btAuditor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.openmedical.brregister.pojo.vo.AddAuditorVo;
import com.tedu.openmedical.brregister.service.BrRegisterService;
import com.tedu.openmedical.btAuditor.pojo.BtAuditor;
import com.tedu.openmedical.btAuditor.mapper.BtAuditorMapper;
import com.tedu.openmedical.btAuditor.pojo.vo.AuditorSelectAllVo;
import com.tedu.openmedical.btAuditor.pojo.vo.AuditorSelectOneVo;
import com.tedu.openmedical.btAuditor.service.BtAuditorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tedu.openmedical.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 病案借阅审核表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-08-11
 */
@Service
public class BtAuditorServiceImpl extends ServiceImpl<BtAuditorMapper, BtAuditor> implements BtAuditorService {
    @Autowired
    private BrRegisterService brRegisterService;

    private static final Logger log = LoggerFactory.getLogger(BtAuditorServiceImpl.class);

    //addAuditor：新增审核表
    @Override
    public void addAuditor(AddAuditorVo addAuditorVo) {
        BtAuditor btAuditor = new BtAuditor();
        btAuditor.setBtRegisterId(addAuditorVo.getBrId());
        btAuditor.setBtMedicalRecordStatus(addAuditorVo.getBrMedicalRecordStatus());
        btAuditor.setBtGlobalSerial(Util.getFlowingNumber());
        log.info("btAuditor,{}",btAuditor);

        int insert = baseMapper.insert(btAuditor);
        log.info("insert,{}",insert);
    }
    //selectAll：查询全部
    @Override
    public List<AuditorSelectAllVo> selectAll() {

        return baseMapper.selectAll();
    }
    //selectOne：根据id查询
    @Override
    public AuditorSelectOneVo selectOne(String id) {

        AuditorSelectOneVo auditorSelectOneVo = baseMapper.queryById(id);
        log.info("id123,{}",id);

        return auditorSelectOneVo;
    }
    // 更新状态 0-待审核，1-待出库 2-已出库 3-已取消 4-已驳回
    //updateStatusPass：1-待出库
    @Override
    public void updateStatusPass(String id) {
        //组织查询条件
        QueryWrapper<BtAuditor> btQueryWrapper = new QueryWrapper<>();
        btQueryWrapper.eq("bt_id",id);
        BtAuditor btAuditor =  baseMapper.selectOne(btQueryWrapper);
        //更新病案状态：  1-待出库
        btAuditor.setBtMedicalRecordStatus(1);
        baseMapper.updateById(btAuditor);
        brRegisterService.updateStatusPass(btAuditor.getBtRegisterId());
    }
    //updateStatusReject：4-已驳回
    @Override
    public void updateStatusReject(String id, String rejectReason) {

        //组织查询条件
        QueryWrapper<BtAuditor> btQueryWrapper = new QueryWrapper<>();
        btQueryWrapper.eq("bt_id",id);
        BtAuditor btAuditor =  baseMapper.selectOne(btQueryWrapper);
        //更新病案状态：  4-已驳回
        btAuditor.setBtMedicalRecordStatus(4);

        baseMapper.updateById(btAuditor);
        brRegisterService.updateStatusReject(btAuditor.getBtRegisterId(),rejectReason);

    }
    //updateStatusPass：4-待出库

    @Override
    public void updateStatusCancel(String id) {
        //组织查询条件
        QueryWrapper<BtAuditor> btQueryWrapper = new QueryWrapper<>();
        btQueryWrapper.eq("bt_id",id);
        BtAuditor btAuditor =  baseMapper.selectOne(btQueryWrapper);
        //更新病案状态：  3-已取消
        btAuditor.setBtMedicalRecordStatus(3);
        baseMapper.updateById(btAuditor);
        brRegisterService.updateStatusCancel(btAuditor.getBtRegisterId());
    }

}
